<?php

/**
 * @file
 * A collection of built-in select list options for Webform.
 */

/**
 * Private implementation of hook_webform_select_options_info().
 *
 * @see webform_webform_select_options_info()
 */
function _webform_options_info() {
  $items = array();

  $items['days'] = array(
    'title' => t('Days of the week'),
    'options callback' => 'webform_options_days',
    'file' => 'includes/webform.options.inc',
  );

  $items['countries'] = array(
    'title' => t('Countries'),
    'options callback' => 'webform_options_countries',
    'file' => 'includes/webform.options.inc',
  );

  $items['united_states'] = array(
    'title' => t('US states'),
    'options callback' => 'webform_options_united_states',
    'file' => 'includes/webform.options.inc',
  );
  
  $items['languages'] = array(
    'title' => t('Languages'),
    'options callback' => 'webform_options_languages',
    'file' => 'includes/webform.options.inc',
  );
  
  // change to sites from Phase2.0
  $items['locations'] = array(
    'title' => t('Location Sites'),
    'options callback' => 'webform_options_locations',
    'file' => 'includes/webform.options.inc',
  );
  
  $items['location_citys'] = array(
    'title' => t('Location Citys'),
    'options callback' => 'webform_options_locations_citys',
    'file' => 'includes/webform.options.inc',
  );
  
  $items['dynamic_dabatase'] = array(
    'title' => t('Dynamic Dabatase'),
    'options callback' => 'webform_options_dynamic_dabatase',
    'file' => 'includes/webform.options.inc',
  );

  return $items;
}

function _webform_options_prebuild_translations($component) {
  $lists = array();
  if (isset($component['extra']['lang_wf'])) {
    $lang = _get_webform_translation();
    $lid  = isset($lang['lid'][$component['extra']['lang_wf']]) ? $lang['lid'][$component['extra']['lang_wf']] : 0;  
    $translations = _get_webform_component_translation($component['nid'], $lid, $component['cid']); //print_r($translations);exit;
    if (!empty($translations['items'])) {
	  $temp_arr = array();
	  $temp_arr = explode("\n", trim($translations['items'])); //print_r($temp_arr);exit;
	  foreach ($temp_arr as $value) {
	    $value_temp = explode("|", $value);
		if (count($value_temp) == 2) {
		  $lists[$value_temp[0]] = $value_temp[1];
		}
	  }
    }
  }
  
  //print_r($lists);exit;
  return $lists;
}

function webform_options_languages($component, $flat, $filter, $arguments) {
  $lists_translate = _webform_options_prebuild_translations($component);
  if (count($lists_translate) > 0) {
    return $lists_translate;
  }
  
  $languages = array();
  $lang = _get_webform_translation();
  $languages += $lang['name'];
  
  return $languages;
}

function webform_options_locations($component, $flat, $filter, $arguments) {
  $locations = array(); //print_r($component);exit;
  	
  // Load location site list by nid
  $country = WEBFORM_DEFAULT_COUNTRY;
  if (isset($component['nid'])) {
      $country_arr = webform_get_country($component['nid']);
      if (isset($country_arr['key']))
        $country = $country_arr['key'];
  }
  
  //phase3 add robert
  if (isset($_REQUEST['country'])) {
    $country = $_REQUEST['country'];
  }
  //Edit webform page
  if (arg(0) == 'node' && arg(2) == 'submission') {
    $sid = arg(3);
    $nid = arg(1);
    $country_data = submission_get_country($sid,$nid);
    $country = $country_data['key'];
  }//end Edit webform page
  
  //if done?sid=
  if (isset($component['country_key'])) {
    $country = $component['country_key'];
  }
  
  // stone get country from file upload
  if(isset($_POST['submitted']['country_of_domestic_transfer'])){
    $country = check_plain($_POST['submitted']['country_of_domestic_transfer']);
  }
  //end phase3

  //$data = location_get_list($country);
  $data = location_site_get_list($country); 
  foreach ($data as $value) {
	  $locations[$value->site_code] = $value->site_name.' '.$value->site_code;
  }
  if($country=='CN'){
      $locations['None'] = t('None of the above');
  }
  if($country=='ID'){
      $locations['Others'] = t('Others');
  }
  return $locations;
  
}

function webform_options_locations_citys($component, $flat, $filter, $arguments) {
  $citys = array();
  // Load location city list by nid
  $country = WEBFORM_DEFAULT_COUNTRY;
  if (isset($component['nid'])) {
      $country_arr = webform_get_country($component['nid']);
      if (isset($country_arr['key']))
        $country = $country_arr['key'];
  }
  //phase3 add robert
  if (isset($_REQUEST['country'])) {
    $country = $_REQUEST['country'];
  }
  //Edit webform page
  if (arg(0) == 'node' && arg(2) == 'submission') {
    $sid = arg(3);
    $nid = arg(1);
    $country_data = submission_get_country($sid,$nid);
    $country = $country_data['key'];
  }//end Edit webform page
  
  //if done?sid=
  if (isset($component['country_key'])) {
    $country = $component['country_key'];
  }
  //end if done?sid=
  
  // stone get country from file upload
  if(isset($_POST['submitted']['country_of_domestic_transfer'])){
    $country = check_plain($_POST['submitted']['country_of_domestic_transfer']);
  }
  //end phase3
  
  $data = location_city_get_list($country); 
  foreach ($data as $value) {
	  $citys[$value->city_name] = $value->city_name;
  }
  
  // unset the 'Others' from db and add in the end of the array
  if (isset($citys[LOCATION_CITY_OTHERS])) unset($citys[LOCATION_CITY_OTHERS]);
  $citys[LOCATION_CITY_OTHERS] = t('Others');
  
  return $citys;
}

function webform_options_dynamic_dabatase($component, $flat, $filter, $arguments) {
  global $databases;
  
  //echo 'dynamic dabatase API:';print_r($component);exit;
  $lists = array();
  //$lists = array('1' => t('Test 1'), '2' => t('Test 2'),'3' => t('Test 3'),);
  if (isset($component['extra']['sqlitems'])) {
      $db  = $component['extra']['sqlitems']['db'];
  	  $sql = $component['extra']['sqlitems']['sql_input'];
	  
	  if (is_array($databases[$db])) {
        db_set_active($db);
	    if (!empty($sql)) {
          $attributes = array(
    	  //'target' => $db,
    	  'fetch' => PDO::FETCH_ASSOC,
  	      );
	      $result = db_query($sql, array(), $attributes);
          foreach ($result as $row){
	        $values = array_values($row);	  
  	        $lists[$values[0]] = $values[1];  
          }
	    }
        db_set_active();
      }
  }//print_r($lists);exit;
  
  return $lists;
}

/**
 * Option list containing the days of the week.
 */
function webform_options_days($component, $flat, $filter, $arguments) {
  //print_r($component);exit;
  $lists_translate = _webform_options_prebuild_translations($component);
  if (count($lists_translate) > 0) {
    return $lists_translate;
  }
  
  $days = array(
    'sunday' => t('Sunday'),
    'monday' => t('Monday'),
    'tuesday' => t('Tuesday'),
    'wednesday' => t('Wednesday'),
    'thursday' => t('Thursday'),
    'friday' => t('Friday'),
    'saturday' => t('Saturday'),
  );

  // Order according to site settings for first day.
  if ($first_day = variable_get('date_first_day', 0)) {
    $week = array_splice($days, $first_day);
    $days = array_merge($week, $days);
  }

  return $days;
}

/**
 * Options list containing country names.
 */
function webform_options_countries($component, $flat, $filter, $arguments) {
  include_once DRUPAL_ROOT . '/includes/locale.inc';
  return country_get_list();
}

/**
 * Options list containing United States states and territories.
 */
function webform_options_united_states($component, $flat, $filter, $arguments) {
  return array(
    'AL' => t('Alabama'),
    'AK' => t('Alaska'),
    'AS' => t('American Samoa'),
    'AZ' => t('Arizona'),
    'AR' => t('Arkansas'),
    'CA' => t('California'),
    'CO' => t('Colorado'),
    'CT' => t('Connecticut'),
    'DE' => t('Delaware'),
    'DC' => t('District of Columbia'),
    'FL' => t('Florida'),
    'GA' => t('Georgia'),
    'GU' => t('Guam'),
    'HI' => t('Hawaii'),
    'ID' => t('Idaho'),
    'IL' => t('Illinois'),
    'IN' => t('Indiana'),
    'IA' => t('Iowa'),
    'KS' => t('Kansas'),
    'KY' => t('Kentucky'),
    'LA' => t('Louisiana'),
    'ME' => t('Maine'),
    'MH' => t('Marshall Islands'),
    'MD' => t('Maryland'),
    'MA' => t('Massachusetts'),
    'MI' => t('Michigan'),
    'MN' => t('Minnesota'),
    'MS' => t('Mississippi'),
    'MO' => t('Missouri'),
    'MT' => t('Montana'),
    'NE' => t('Nebraska'),
    'NV' => t('Nevada'),
    'NH' => t('New Hampshire'),
    'NJ' => t('New Jersey'),
    'NM' => t('New Mexico'),
    'NY' => t('New York'),
    'NC' => t('North Carolina'),
    'ND' => t('North Dakota'),
    'MP' => t('Northern Marianas Islands'),
    'OH' => t('Ohio'),
    'OK' => t('Oklahoma'),
    'OR' => t('Oregon'),
    'PW' => t('Palau'),
    'PA' => t('Pennsylvania'),
    'PR' => t('Puerto Rico'),
    'RI' => t('Rhode Island'),
    'SC' => t('South Carolina'),
    'SD' => t('South Dakota'),
    'TN' => t('Tennessee'),
    'TX' => t('Texas'),
    'UT' => t('Utah'),
    'VT' => t('Vermont'),
    'VI' => t('Virgin Islands'),
    'VA' => t('Virginia'),
    'WA' => t('Washington'),
    'WV' => t('West Virginia'),
    'WI' => t('Wisconsin'),
    'WY' => t('Wyoming'),
  );
}
